package cn.jiangjiesheng.bootstrap.integration.audit.repository;

import cn.jiangjiesheng.bootstrap.integration.audit.po.AuditLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class AuditLogRepository {

    @Qualifier("jdbcTemplateIntegrationEmbedded")
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    public void createTable() {
        String ddl = "CREATE TABLE IF NOT EXISTS audit_log (\n" +
                "  id TEXT PRIMARY KEY,\n" +
                "  method TEXT,\n" +
                "  path TEXT,\n" +
                "  tag TEXT,\n" +
                "  javaPath TEXT,\n" +
                "  request TEXT,\n" +
                "  response TEXT,\n" +
                "  headers TEXT,\n" +
                "  queryParams TEXT,\n" +
                "  dateTime TEXT,\n" +
                "  consumption TEXT\n" +
                ");";
        jdbcTemplate.execute(ddl);
    }

    public void save(AuditLog auditLog) {
        String dml = "INSERT INTO audit_log (id, method, path,tag, javaPath, request, response, headers, queryParams, dateTime, consumption) \n" +
                "VALUES (" +
                "'" + auditLog.getId() + "'," +
                "'" + auditLog.getMethod() + "'," +
                "'" + auditLog.getPath() + "'," +
                "'" + auditLog.getTag() + "'," +
                "'" + auditLog.getJavaPath() + "'," +
                "'" + auditLog.getRequest() + "'," +
                "'" + auditLog.getResponse() + "'," +
                "'" + auditLog.getHeaders() + "'," +
                "'" + auditLog.getQueryParams() + "'," +
                "'" + auditLog.getDateTime() + "'," +
                "'" + auditLog.getConsumption() + "'" +
                ");";
        jdbcTemplate.execute(dml);
    }

    public List<AuditLog> select(int offset, int limit) {
        return jdbcTemplate.query("SELECT * FROM audit_log ORDER BY id DESC LIMIT " + offset + "," + limit + ";", (rs, rowNum) -> {
            AuditLog auditLog = new AuditLog();
            auditLog.setId(rs.getString("id"));
            auditLog.setMethod(rs.getString("method"));
            auditLog.setPath(rs.getString("path"));
            auditLog.setTag(rs.getString("tag"));
            auditLog.setJavaPath(rs.getString("javaPath"));
            auditLog.setRequest(rs.getString("request"));
            auditLog.setResponse(rs.getString("response"));
            auditLog.setHeaders(rs.getString("headers"));
            auditLog.setQueryParams(rs.getString("queryParams"));
            auditLog.setDateTime(rs.getString("dateTime"));
            auditLog.setConsumption(rs.getString("consumption"));
            return auditLog;
        });
    }

    public void deleteAll() {
        String dml = "DELETE FROM audit_log;";
        jdbcTemplate.execute(dml);
    }
}
